package enron;

public class Appliance {

	private Long wattage = 0L;
	private String name = "";
	private Boolean turnedOn = false;
	private BuyerAgent owner = null;
	
	public Appliance(String name){
		this(name,0L,null,false);
	}
	
	public Appliance(String name, boolean turnedOn){
		this(name,0L,null,turnedOn);
	}
	
	public Appliance(String name, BuyerAgent owner){
		this(name,0L,owner,false);
	}
	
	public Appliance(String name, BuyerAgent owner, boolean turnedOn) {
		this(name,0L,owner,turnedOn);
	}
	
	public Appliance(String name, Long wattage){
		this(name,wattage,null,false);
	}
	
	public Appliance(String name, Long wattage, boolean turnedOn) {
		this(name,wattage,null,turnedOn);
	}
	
	public Appliance(String name, Long wattage, BuyerAgent owner){
		this(name,wattage,owner,false);
	}
	
	public Appliance(String name, Long wattage, BuyerAgent owner, boolean turnedOn) {
		this.name = name;
		this.wattage = wattage;
		this.owner = owner;
		this.turnedOn = turnedOn;
		if(turnedOn && this.owner!=null) {
			this.owner.addPower(wattage);
		}
	}

	
	@Override
	public String toString() {
		return name + "(Appliance) { wattage: " + wattage + ", Owner: " + owner + ", turned " + (turnedOn?"on":"off") + "}";
	}

	public void setOwner(BuyerAgent owner) {
		this.owner = owner;
	}
	
	public Long getWattage() {
		return wattage;
	}
	
	public void setWattage(Long wattage) {
		this.wattage = wattage;
	}

	public Boolean isTurnedOn() {
		return turnedOn;
	}

	public void setTurnedOn(Boolean turnedOn) {
		this.turnedOn = turnedOn;

		if(this.owner!=null) {
	        if (this.turnedOn) {
	        	this.owner.addPower(this.wattage);
	        	//System.out.println(this.owner.getLocalName() + " turned on " + this.getName() + ", power use now " + this.owner.getPower() + "W");
	        } else {
	        	this.owner.removePower(this.wattage);
	        	//System.out.println(this.owner.getLocalName() + " turned off " + this.getName() + ", power use now " + this.owner.getPower() + "W");
	        }
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplay() {
		return name + " (" + wattage + "W)";
	}
}
